#!/bin/bash
# ================================================
# 缤纷云对象存储一键挂载脚本（多平台兼容版）
# 功能：自动安装 bitifs 客户端，配置凭证并挂载对象存储
# 支持系统：Debian 12 / Ubuntu 22.04 / CentOS 7
# 版本: 2.1
# 作者: https://blog.hx99.net | https://gitee.com/cncsrf/bitiful-oss
# ================================================

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 彩色输出函数
info() { echo -e "${GREEN}[信息]${NC} $1"; }
warn() { echo -e "${YELLOW}[警告]${NC} $1"; }
error() { echo -e "${RED}[错误]${NC} $1"; exit 1; }
title() { echo -e "${BLUE}$1${NC}"; }

# 检查root权限
[ "$(id -u)" != "0" ] && error "此脚本需要root权限执行!"

# 欢迎信息
title "================================================"
title "  缤纷云(bitiful.com)对象存储一键挂载脚本"
title "================================================"
info " 功能说明:"
info " 1. 自动下载并安装 bitifs 客户端"
info " 2. 配置访问凭证(AK/SK)"
info " 3. 将指定的存储桶挂载到本地目录"
info " 4. 配置开机自动挂载服务"
info ""
info " 支持系统: Debian12 / Ubuntu22.04 / CentOS7"
warn " 注意: 其它系统未经测试，可能不兼容"
info ""
info " AK/SK获取地址: https://console.bitiful.com/accessKey"
info " 项目仓库地址: https://gitee.com/cncsrf/bitiful-oss"
warn ""
warn " 操作前请确保:"
warn " 1. 网络连接正常"
warn " 2. 存储桶可访问"
warn " 3. AK/SK信息正确"
title "================================================"

# 日志记录
log() { info "[$(date '+%Y-%m-%d %H:%M:%S')] $1"; }

# 交互式输入配置
read -p "$(info '请输入 Access Key ID: ')" AWS_ACCESS_KEY_ID
read -p "$(info '请输入 Secret Access Key: ')" AWS_SECRET_ACCESS_KEY
read -p "$(info '请输入存储桶名称: ')" BUCKET_NAME
read -p "$(info '请输入挂载路径(默认:/mnt/bitiful-oss): ')" MOUNT_PATH
MOUNT_PATH=${MOUNT_PATH:-"/mnt/bitiful-oss"}

# 验证挂载路径
[[ "$MOUNT_PATH" != /* ]] && error "挂载路径必须是绝对路径(以/开头)"
mkdir -p "$MOUNT_PATH" || error "创建挂载目录失败!"

# 安装依赖
log "正在安装系统依赖..."
if grep -qiE "debian|ubuntu" /etc/os-release; then
    apt-get update && apt-get install -y wget fuse
elif grep -qi "centos" /etc/os-release; then
    yum install -y wget fuse
else
    error "不支持的操作系统!"
fi

# 下载 bitifs
log "正在安装 bitifs 客户端..."
wget -O /usr/local/bin/bitifs https://tools.bitiful.com/bitifs/bitifs_linux_amd64 || error "下载 bitifs 失败!"
chmod +x /usr/local/bin/bitifs

# 配置凭证
log "正在配置访问凭证..."
mkdir -p /etc/bitifs
cat > /etc/bitifs/credentials.env <<EOF
export AWS_ACCESS_KEY_ID="$AWS_ACCESS_KEY_ID"
export AWS_SECRET_ACCESS_KEY="$AWS_SECRET_ACCESS_KEY"
EOF
chmod 600 /etc/bitifs/credentials.env

# 测试挂载
log "正在测试挂载..."
source /etc/bitifs/credentials.env
if /usr/local/bin/bitifs "$BUCKET_NAME" "$MOUNT_PATH"; then
    log "测试挂载成功"
    umount "$MOUNT_PATH"
else
    error "测试挂载失败! 请检查:\n1. AK/SK是否正确\n2. 存储桶是否存在\n3. 网络连接"
fi

# 配置系统服务
log "配置开机自动挂载..."
cat > /etc/systemd/system/bitifs-mount.service <<EOF
[Unit]
Description=Mount Bitiful Object Storage ($BUCKET_NAME)
After=network.target

[Service]
Type=oneshot
ExecStart=/bin/bash -c 'source /etc/bitifs/credentials.env && /usr/local/bin/bitifs $BUCKET_NAME $MOUNT_PATH'
ExecStop=/bin/umount $MOUNT_PATH
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable --now bitifs-mount.service || error "服务配置失败!"

# 验证结果
mount | grep -q "$MOUNT_PATH" || error "挂载点未生效!"
log "挂载配置完成!"
title "================================================"
info " 配置摘要:"
info " 存储桶: $BUCKET_NAME"
info " 挂载点: $MOUNT_PATH"
info ""
info " 服务管理命令:"
info " 启动服务: systemctl start bitifs-mount.service"
info " 停止服务: systemctl stop bitifs-mount.service"
info " 查看状态: systemctl status bitifs-mount.service"
info " 查看日志: journalctl -u bitifs-mount.service -f"
info ""
info " 卸载方法:"
info " 1. 停止服务: systemctl stop bitifs-mount.service"
info " 2. 禁用服务: systemctl disable bitifs-mount.service"
info " 3. 卸载挂载: umount $MOUNT_PATH"
info " 4. 删除文件: rm -f /usr/local/bin/bitifs /etc/systemd/system/bitifs-mount.service"
info " 5. 删除配置: rm -rf /etc/bitifs"
info ""
info " 更多帮助请参考项目仓库: https://gitee.com/cncsrf/bitiful-oss"
title "================================================"